package com.zhixuan.autoui.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.LayoutRes;

import com.zhixuan.autoui.R;

/**
 * author: HuZC
 * Date: 2021/11/23
 * Description:
 */
public abstract class BaseTestActivity extends Activity {
    protected final String TAG = getClass().getName();

    private TextView tvMessage;

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity_base_test_frame);

        tvMessage = findViewById(R.id.tv_msg);
        FrameLayout flContainer = findViewById(R.id.fl_container);
        int onContentLayout = onContentLayout();
        if (onContentLayout != 0) {
            LayoutInflater.from(this).inflate(onContentLayout, flContainer, true);
        }

        tvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvMessage.setText(null);
            }
        });
        onCreated(savedInstanceState);
    }

    @LayoutRes
    protected abstract int onContentLayout();

    protected abstract void onCreated(Bundle savedInstanceState);

    public void addMessage(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String s = tvMessage.getText().toString();
                tvMessage.setText(s + "\n" + text);
            }
        });
    }

    public void setMessage(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvMessage.setText(text);
            }
        });
    }

    private Toast toast;

    public void toast(String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (toast == null) {
                    toast = Toast.makeText(BaseTestActivity.this, text, Toast.LENGTH_SHORT);
                } else {
                    toast.setText(text);
                }
                toast.show();
            }
        });
    }
}
